#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pika
import time
def server():
    # ######################### 生产者 #########################
    credentials = pika.PlainCredentials('guest', 'guest')
    #链接rabbit服务器（localhost是本机，如果是其他服务器请修改为ip地址）
    connection = pika.BlockingConnection(pika.ConnectionParameters('127.0.0.1',5672,'/',credentials))
    #创建频道
    channel = connection.channel()
    # 声明消息队列，消息将在这个队列中进行传递。如果将消息发送到不存在的队列，rabbitmq将会自动清除这些消息。如果队列不存在，则创建
    # channel.queue_declare(queue='alarm_ehl_alarmservice',durable=True)
    #exchange -- 它使我们能够确切地指定消息应该到哪个队列去。
    #向队列插入数值 routing_key是队列名 body是要插入的内容

    channel.basic_publish(exchange='alarm',
                      routing_key='alarm_ehl_alarmservice',
                      body='Hello World!')
    print("开始队列")
    #缓冲区已经flush而且消息已经确认发送到了RabbitMQ中，关闭链接
    connection.close()

def client():
    credentials = pika.PlainCredentials('guest', 'guest')
    # 链接rabbit
    connection = pika.BlockingConnection(pika.ConnectionParameters('127.0.0.1', 5672, '/', credentials))
    # 创建频道
    channel = connection.channel()
    # 如果生产者没有运行创建队列，那么消费者创建队列
    channel.queue_declare(queue='alarm_ehl')

    def callback(ch, method, properties, body):
        print(" [x] Received %r" % body)
        # time.sleep(1)
        print('ok')
        # ch.basic_ack(delivery_tag=method.delivery_tag)  # 主要使用此代码

    channel.basic_consume(callback,
                          queue='alarm_ehl',
                          no_ack=True)
    channel.basic_consume(callback,
                          queue='alarm_ehl_alarmservice',
                          no_ack=True)

    print(' [*] Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()

if __name__ == '__main__':
    client()
    for i in range(10000000):
        print('ttt')
        time.sleep(10)